<?php
include('../../common.php');
if($_SESSION['log_islogin'] == 'yes' ){

	$name = mysql_real_escape_string($_GET['name']);
	$postcode = intval(mysql_real_escape_string($_GET['postcode']));
	$tpdID = intval(mysql_real_escape_string($_GET['tpdID']));
	if($name != "" && strlen($postcode) == 5 && $tpdID > 0){
		$qList = "SELECT a.post_office AS city, a.state_code AS state 
					FROM lookup_postcode a  WHERE postcode='$postcode' LIMIT 0,1";
		$rList = $db->sql_list($qList);	

		$city = $rList['city'];
		$state = $rList['state'];
		if(strlen($city) > 0  && strlen($state) > 0 ){
		
			$entReceiverSql= "select * from entity where ent_short_name = '$name'";
			$entReceiverGet = $db->sql_list($entReceiverSql);
			$receiverId = $entReceiverGet['ent_id'];
			
			if($receiverId > 0){
																		
				$sqlUpdReceiverEnt = "UPDATE entity set ent_add_postcode ='$postcode', ent_add_city='$city ', ent_add_state='$state' 
									 WHERE ent_id='$receiverId' ";
				$resReceiverEnt = $db->sql_query($sqlUpdReceiverEnt);
				
				$sqlUpdTPD = "  UPDATE transport_package_delivering  set tpd_receiver_ent_id = '$receiverId' 
								   where tpd_id = '$tpdID' ";
				$resUpdTPD = $db->sql_query($sqlUpdTPD);
				
				
			}else{
			
				$sqlInsertReceiverEnt = "INSERT INTO entity 
							(ent_name,ent_short_name,
								ent_add_postcode,ent_add_city,ent_add_state,
								ent_created_date,ent_created_by) 
							VALUES
							('$name','$name',
							 '$postcode','$city','$state',
							 NOW(),'$_SESSION[log_userlogin_id]')";
				$resReceiverEnt = $db->sql_query($sqlInsertReceiverEnt);
			}														
			if($resReceiverEnt)
				echo '1';
			else
				echo '0';
		}else
			echo '0';
	}else
			echo '0';
}else{
	echo 'Dude!';
}
?>